﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;


namespace PRE.Catalog
{
    public partial class FrmCapTheDocGia : DevExpress.XtraEditors.XtraForm
    {
        #region Variable
        DAL.EntitiesDAL.TheDocGiaDAL _dal = new DAL.EntitiesDAL.TheDocGiaDAL();
        DAL.EntitiesDAL.LoaiDocGiaDAL _dal2 = new DAL.EntitiesDAL.LoaiDocGiaDAL();
        BLL.EntitiesBLL.TheDocGiaBLL _bll = new BLL.EntitiesBLL.TheDocGiaBLL();

        DataTable _dt = new DataTable();
        DataTable _dt2 = new DataTable();
       

        #endregion
        public FrmCapTheDocGia()
        {
            InitializeComponent();
        }

        private void cbo_CapThe_Click(object sender, EventArgs e)
        {
          
            EnableButton(false);
            ReadOnlyControl(false);
            txt_MaDocGia.Text = Add();

            _dt = _dal2.Select();

            if (_dt != null)
            {
                cbo_MaLoaiDocGia.Properties.DataSource = _dt;
            }

            dte_NgayBatDau.EditValue = DateTime.Now;
            dte_NgayHetHan.EditValue = DateTime.Now.AddYears(1);
            txt_MaDocGia.Properties.ReadOnly = true;
            dte_NgayBatDau.Properties.ReadOnly = true;
            dte_NgayHetHan.Properties.ReadOnly = true;
            txt_HoVaTen.Focus();
        }

        private string Add()
        {
            string ma = "";
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                   ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                }
            return ma;
          }

        private void FrmCapTheDocGia_Load(object sender, EventArgs e)
        {
            EnableButton();
            ResetText();
            ReadOnlyControl();
        }

        private void EnableButton(bool isTrue = true)
        {
            cbo_CapThe.Visible = isTrue;
         
            cbo_InThe.Visible = !isTrue;
            cbo_NhapLai.Visible = !isTrue;

        }

        private void ResetText()
        {
            txt_MaDocGia.ResetText();
            txt_HoVaTen.ResetText();
            dte_NgaySinh.ResetText();
            radio_Gioitinh.ResetText();
            txt_SoCMND.ResetText();
            txt_DiaChi.ResetText();
            txt_SoDienThoai.ResetText();
            txt_NgheNghiep.ResetText();
            txt_NoiCongTac.ResetText();
            dte_NgayBatDau.ResetText();
            dte_NgayHetHan.ResetText();
            cbo_MaLoaiDocGia.ResetText();
          
        }

        private void cbo_NhapLai_Click(object sender, EventArgs e)
        {
            ResetText();
            ReadOnlyControl(false);
            txt_MaDocGia.Text = Add();
            txt_MaDocGia.Properties.ReadOnly = true;
            txt_HoVaTen.Focus();
  
        }

        private void ReadOnlyControl(bool isReadOnly = true)
        {

            txt_MaDocGia.Properties.ReadOnly = isReadOnly;
            txt_HoVaTen.Properties.ReadOnly = isReadOnly;
            dte_NgaySinh.Properties.ReadOnly = isReadOnly;
            radio_Gioitinh.Properties.ReadOnly = isReadOnly;
            txt_SoCMND.Properties.ReadOnly = isReadOnly;
            txt_DiaChi.Properties.ReadOnly = isReadOnly;
            txt_SoDienThoai.Properties.ReadOnly = isReadOnly;
            txt_NgheNghiep.Properties.ReadOnly = isReadOnly;
            txt_NoiCongTac.Properties.ReadOnly = isReadOnly;
            dte_NgayBatDau.Properties.ReadOnly = isReadOnly;
            dte_NgayHetHan.Properties.ReadOnly = isReadOnly;
            cbo_MaLoaiDocGia.Properties.ReadOnly = isReadOnly;

        }

        private void cbo_Refresh_Click(object sender, EventArgs e)
        {
            FrmCapTheDocGia_Load(sender, e);
        }

        private void cbo_InThe_Click(object sender, EventArgs e)
        {
            Catalog.FrmBase x = new Catalog.FrmBase();

            var name_HoTen = x.Proper(txt_HoVaTen.Text);
            var name_DiaChi = x.Proper(txt_DiaChi.Text);
            var name_NgheNghiep = x.Proper(txt_NgheNghiep.Text);
            var name_NoiCongTac = x.Proper(txt_NoiCongTac.Text);

            var o = new DAL.Entities.TheDocGia()
            {
                MADOCGIA = txt_MaDocGia.Text,
                HODOCGIA = name_HoTen,
                NGAYSINH = Convert.ToDateTime(dte_NgaySinh.EditValue),
                GIOITINH = Convert.ToBoolean(radio_Gioitinh.EditValue),
                SCMND = txt_SoCMND.Text,
                DIACHI = name_DiaChi,
                DIENTHOAI = txt_SoDienThoai.Text,
                NGHENGHIEP = name_NgheNghiep,
                NOICONGTAC = name_NoiCongTac,
                NGAYBATDAU = Convert.ToDateTime(dte_NgayBatDau.EditValue),
                NGAYHETHAN = Convert.ToDateTime(dte_NgayHetHan.EditValue),
                MALOAIDG = Convert.ToString(cbo_MaLoaiDocGia.EditValue)

            };

            var oki = _dal.Insert(o);
            if (oki)
            {
                XtraMessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                XtraMessageBox.Show("Lỗi! Không thêm được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            string _NgayKy = "TP.Cần Thơ, ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
            DateTime _NgaySinh = Convert.ToDateTime(dte_NgaySinh.EditValue);
            DateTime _Ngaybatdau = Convert.ToDateTime(dte_NgayBatDau.EditValue);
            DateTime _Ngayhethan = Convert.ToDateTime(dte_NgayHetHan.EditValue);

            Reports.FrmReportView frm = new Reports.FrmReportView();
            frm._load_XtraTheDocGia(_dt, txt_MaDocGia.Text, x.Proper(txt_HoVaTen.Text), _NgaySinh.ToString("dd/MM/yyyy")
                , x.Proper(txt_NgheNghiep.Text), x.Proper(txt_DiaChi.Text), _Ngaybatdau.ToString("dd-MM-yyyy"), _Ngayhethan.ToString("dd-MM-yyyy"), _NgayKy.ToString());
            frm.Show();
        }





        private void cbo_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
